/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file creates Figures A1 and A2 displaying the sizes of adoption cohorts.
*/

*Define directories and set working directory
/* NOTE - To replicate our results, you need to change the root directory address in the next line */
global dr_root = "C:\Users\Micah\Dropbox\Irrigation technology transition\final revisions for conditional acceptance\replication materials"
global dr_code = "${dr_root}\code"
global dr_data = "${dr_root}\data"
global dr_output = "${dr_root}\outputs"
global dr_output_main = "${dr_root}\outputs\main_text"
global dr_output_app = "${dr_root}\outputs\appendices"
global dr_output_log = "${dr_root}\outputs\logs"
global dr_temp = "${dr_root}\data\intermediate"
cd "${dr_root}"

***************************** Figure A1 ****************************************
*Load flood to cp or lepa dataset
import delimited using "$dr_temp\flood_cporlepa_prepped.csv", clear
keep wr_group cohort_flood_cplepa
drop if cohort_flood_cplepa==0
duplicates drop
hist cohort_flood_cplepa, ///
	name(hist_flood_cplepa, replace) ///
	title("Flood to center pivot", size(small) position(11)) ///
	xtitle("") ytitle("", size(small) angle(vertical)) legend(off) ///
	xsize(2) ysize(2.8) freq  discrete ///
	xlabel(1991(4)2019, labsize(small) angle(vertical)) xlabel(1991(4)2019, labsize(small) angle(vertical)) ///
	ylabel(0(50)200, labsize(small))

*Load cp to LEPA
import delimited using "$dr_temp\cp_lepa_prepped.csv", clear
keep wr_group cohort_cp_lepa
drop if cohort_cp_lepa==0
duplicates drop
hist cohort_cp_lepa, ///
	title("Traditional center pivot to LEPA", size(small) position(11)) ///
	name(hist_cp_lepa, replace) ///
	xtitle("") legend(off) xtitle("") ytitle("", size(small) angle(vertical)) xsize(2) ysize(2.8) ///
	freq  discrete ///
	xlabel(1991(4)2019, labsize(small) angle(vertical)) ///
	ylabel(0(200)1000, labsize(small))

*combine graphs
graph combine hist_flood_cplepa hist_cp_lepa, ///
	rows(1) name(comb_gr, replace) ///
	title("Cohort size", size(medsmall)) ///
	b2title("Year of technology change", size(small)) l2title("Number of water right groups", size(small))
graph export "${dr_output_app}/figureA1.tif", replace wid(6500) height(4500)

***************************** Figure A2 ****************************************
*Now do flood to only cp
import delimited using "$dr_temp\flood_cporlepa_prepped.csv", clear
xtset wr_group wua_year
	keep if num_switch_flood_cp==1
	keep wr_group cohort_flood_cplepa
	drop if cohort_flood_cplepa==0
	duplicates drop
	hist cohort_flood_cplepa, ///
		name(hist_flood_onlycp, replace) ///
		title("Flood to traditional center pivot", size(small) position(11)) ///
		xtitle("") ytitle("", size(small) angle(vertical)) legend(off) ///
		xsize(2) ysize(2.8) freq  discrete ///
		xlabel(1991(4)2019, labsize(small) angle(vertical)) xlabel(1991(4)2019, labsize(small) angle(vertical)) ///
		ylabel(0(50)150, labsize(small))

*Now do flood to only lepa
import delimited using "$dr_temp\flood_cporlepa_prepped.csv", clear
xtset wr_group wua_year
	keep if num_switch_flood_lepa==1
	keep wr_group cohort_flood_cplepa
	drop if cohort_flood_cplepa==0
	duplicates drop
	hist cohort_flood_cplepa, ///
		name(hist_flood_onlylepa, replace) ///
		title("Flood to LEPA", size(small) position(11)) ///
		xtitle("") ytitle("", size(small) angle(vertical)) legend(off) ///
		xsize(2) ysize(2.8) freq  discrete ///
		xlabel(1991(4)2019, labsize(small) angle(vertical)) xlabel(1991(4)2019, labsize(small) angle(vertical)) ///
		ylabel(0(50)150, labsize(small))

		*combine graphs
graph combine hist_flood_onlycp hist_flood_onlylepa, ///
	rows(1) name(comb_gr, replace) ///
	title("Cohort size", size(medsmall)) ///
	b2title("Year of technology change", size(small)) l2title("Number of water right groups", size(small))
graph export "${dr_output_app}/figureA2.tif", replace wid(6500) height(4500)
